package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.service.CheckItemService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/checkitem")
@Slf4j
public class CheckItemController {

    @DubboReference
    CheckItemService checkItemService;
    /**
     * @Author: JASON
     * @Date: 10:19 2021/1/4
     * @Parms [queryPageBean]
     * @ReturnType: com.itheima.health.entity.PageResult
     * @Description: 分页查询
    */
    @PreAuthorize("hasAuthority('CHECKITEM_QUERY')")
    @GetMapping("/findByPage")
    public Result findByPage(QueryPageBean queryPageBean){
        log.info("Controller[检查项]分页查询"+queryPageBean);
        PageResult pageResult=checkItemService.findByPage(queryPageBean);
        return new Result(true,MessageConst.QUERY_CHECKITEM_SUCCESS,pageResult);
    }
    /**
     * @Author: JASON
     * @Date: 11:36 2021/1/4
     * @Parms [checkItem]
     * @ReturnType: com.itheima.health.entity.Result
     * @Description: 添加checkitem
    */
    @PreAuthorize("hasAuthority('CHECKITEM_ADD')")
    @PostMapping("/add")
    public Result add(@RequestBody CheckItem checkItem){
        log.info("Controller[检查项]添加数据"+checkItem);
        checkItemService.add(checkItem);
        return new Result(true,MessageConst.ADD_CHECKITEM_SUCCESS);
    }

    /**
     * @Author: JASON
     * @Date: 12:15 2021/1/4
     * @Parms [id]
     * @ReturnType: com.itheima.health.entity.Result
     * @Description: 根据id删除检查项
    */
    @PreAuthorize("hasAuthority('CHECKITEM_DELETE')")
    @PostMapping("/delete")
    public Result delete(Integer id){
        log.info("Controller[检查项]根据id删除数据"+id);
        checkItemService.delete(id);
        return new Result(true,MessageConst.DELETE_CHECKITEM_SUCCESS);
    }

    /**
     * @Author: JASON
     * @Date: 14:50 2021/1/4
     * @Parms [id]
     * @ReturnType: com.itheima.health.entity.Result
     * @Description: 根据id查找检查项
    */
    @PreAuthorize("hasAuthority('CHECKITEM_QUERY')")
    @GetMapping("/findById")
    public Result findById(Integer id){
        log.info("Controller[检查项]根据id查找数据"+id);
        CheckItem checkItem=checkItemService.findById(id);
        return new Result(true,MessageConst.QUERY_CHECKITEM_SUCCESS,checkItem);
    }


    /**
     * @Author: JASON
     * @Date: 15:05 2021/1/4
     * @Parms [checkItem]
     * @ReturnType: void
     * @Description: 根据id修改检查项
     */
    @PreAuthorize("hasAuthority('CHECKITEM_EDIT')")
    @PostMapping("/edit")
    public Result edit(@RequestBody CheckItem checkItem){
        log.info("Controller[检查项]编辑"+checkItem);
        checkItemService.edit(checkItem);
        return new Result(true,MessageConst.EDIT_CHECKITEM_SUCCESS);
    }

    /**
     * @Author: JASON
     * @Date: 14:40 2021/1/5
     * @Parms [queryPageBean]
     * @ReturnType: com.itheima.health.entity.Result
     * @Description: 检查组新增时检查项显示
    */
    @PreAuthorize("hasAuthority('CHECKITEM_QUERY')")
    @GetMapping("/findAll")
    public Result findAll(){
        log.info("Controller[检查项]查询所有");
        List<CheckItem> list =checkItemService.findAll();
        return new Result(true,MessageConst.QUERY_CHECKITEM_SUCCESS,list);
    }
}
